package com.java.servlet.web.mail.service;

import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

import com.java.servlet.web.mail.dao.MailDao;
import com.java.servlet.web.mail.dao.MailDaoImpl;
import com.java.servlet.web.mail.vo.MailPropertiesVo;
import com.java.servlet.web.mail.vo.MailVo;

public class MailServiceImpl implements MailService {
	private static final Logger log = Logger.getLogger(MailServiceImpl.class);
	private MailDao mailDao = new MailDaoImpl();
	@Override
	public void sendMail(MailVo mailVo) {
		Properties props = new Properties();
		String username = null;
		String password = null;
		List<MailPropertiesVo> listProp = mailDao.getListProperties();
		if(listProp.size() > 0){
		for(MailPropertiesVo mailPop : listProp){
			if(mailPop.getDescProp().equals("USERNAME")){
				username = mailPop.getValorProp();
			}
			if(mailPop.getDescProp().equals("PASSWORD")){
				password = mailPop.getValorProp();
			}
			if(mailPop.getDescProp().equals("SMTP AUTO")){
				props.put("mail.smtp.auth", mailPop.getValorProp());
			}
			if(mailPop.getDescProp().equals("SMTP STARTTLS ENABLE")){
				props.put("mail.smtp.starttls.enable", mailPop.getValorProp());
			}
			if(mailPop.getDescProp().equals("HOST")){
				props.put("mail.smtp.host", mailPop.getValorProp());
			}
			if(mailPop.getDescProp().equals("PORT")){
				props.put("mail.smtp.port", mailPop.getValorProp());
			}
		}
		}else{
			username = "pruebas.test.mts@gmail.com";
			password = "prueba12345";
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.smtp.host", "smtp.gmail.com");
			props.put("mail.smtp.port", "587");
		}
		final String user = username;
		final String pass = password;
			Session session = Session.getInstance(props, new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(user, pass);
				}
			  });
			
			try {
				 
				Message message = new MimeMessage(session);
				message.setFrom(new InternetAddress(username));
				message.setRecipients(Message.RecipientType.TO,	InternetAddress.parse(mailVo.getMailPara()));
				message.setRecipients(Message.RecipientType.CC,	InternetAddress.parse(mailVo.getMailCcp()));
				message.setSubject(mailVo.getMailAsunto());
				message.setText(mailVo.getMailContenido());
	 
				Transport.send(message);
	 
				log.info("Done  [send mail]  ");
	 
			} catch (MessagingException e) {
				log.error("Error  [MessagingException]  " + e);
				throw new RuntimeException(e);
			} 
	}
	
	public static void main(String [] args){
		// Recipient's email ID needs to be mentioned.
	      String to = "pruebas.test.mts@gmail.com";

	      // Sender's email ID needs to be mentioned
	      String from = "omar.santiago@meltsan.com";

	      // Assuming you are sending email from localhost
	      String host = "smtp.gmail.com";

	      // Get system properties
	      Properties properties = System.getProperties();

	      // Setup mail server
	      properties.setProperty("mail.smtp.auth", "true");
	      properties.setProperty("mail.smtp.starttls.enable", "true");
	      properties.setProperty("mail.smtp.host", host);
	      properties.setProperty("mail.smtp.port", "587");

	      // Get the default Session object.
	      Session session = Session.getDefaultInstance(properties);

	      try{
	         // Create a default MimeMessage object.
	         MimeMessage message = new MimeMessage(session);

	         // Set From: header field of the header.
	         message.setFrom(new InternetAddress(from));

	         // Set To: header field of the header.
	         message.addRecipient(Message.RecipientType.TO,
	                                  new InternetAddress(to));

	         // Set Subject: header field
	         message.setSubject("This is the Subject Line!");

	         // Now set the actual message
	         message.setText("This is actual message");

	         // Send message
	         Transport.send(message);
	         System.out.println("Sent message successfully....");
	      }catch (MessagingException mex) {
	         mex.printStackTrace();
	      }
	}

}
